/*

 * Created on 2004-6-30

 *

 * To change the template for this generated file go to

 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

 */

package com.stony.core.keygenerator;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.stony.core.bo.HibernateBusinessObject;
import com.stony.core.exception.RunException;

/**
 * 
 * @author george.lv
 * 
 * 
 * 
 * To change the template for this generated type comment go to
 * 
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 * 
 */

public class OracleSequenceGenerator1 extends HibernateBusinessObject {

	private static final Logger logger = Logger
			.getLogger(OracleSequenceGenerator.class);

	private String sequence = "";

	private long key;

	/*
	 * 
	 * (non-Javadoc)
	 * 
	 * 
	 * 
	 * @see com.eapo.common.bo.EAPOBusinessObject#performBusinessLogic()
	 * 
	 */

	public OracleSequenceGenerator1(String sequence) {
		super();
		this.sequence = sequence;
	}

	protected void performBusinessLogic() {

		key = nextVal();

	}

	protected long nextVal() {

		try {

			Connection conn = getSession().connection();

			Statement stmt = conn.createStatement();

			String sequenceSQL = "select " + sequence + ".nextval from dual";

			ResultSet res = stmt.executeQuery(sequenceSQL);

			if (res.next()) {

				long result = res.getLong(1);

				res.close();

				stmt.close();

				return result;

			} else {

				res.close();

				stmt.close();

				throw new RunException("generateKey error");

			}

		} catch (Exception e) {

			logger.error(e);

			throw new RunException("generateKey error");

		}

	}

	public long getKey() {
		return key;
	}

}